package com.buddies.lottery.ui.pay.record;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.buddies.lottery.R;
import com.buddies.lottery.base.list.BaseListActivity;
import com.buddies.lottery.data.bean.PayRecord;
import com.buddies.lottery.ui.lottery.select.LotterySelectActivity;
import com.buddies.lottery.ui.pay.record.select.PayTypeSelectActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;
import java.util.Map;

public class PayRecordActivity extends BaseListActivity<PayRecord, PayRecordContract.Presenter> implements PayRecordContract.View {

    private static final int REQUEST_SELECT_PAY_TYPE = 10001;

    private String mPayType;

    @Override
    protected void initView() {
        super.initView();

        TextView btnFilter = findViewById(R.id.tv_action);
        btnFilter.setText("交易类型筛选");
        btnFilter.setVisibility(View.VISIBLE);
        btnFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayTypeSelectActivity.launch(getActivity(), REQUEST_SELECT_PAY_TYPE);
            }
        });
    }

    @Override
    protected String getHeaderTitle() {
        return "交易记录";
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mPayType = intent.getStringExtra("payType");
        }

        super.initData();

        mPresenter = new PayRecordPresenter(this);
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return new PayRecordAdapter(null);
    }

    @Override
    public void onLoadData(int page) {
        Map<String, Object> params = new HashMap<>();
        params.put("payType", mPayType);
        ((PayRecordContract.Presenter) mPresenter).onLoadData(params, page);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_PAY_TYPE && resultCode == RESULT_OK && data != null) {
            mPayType = data.getStringExtra("payType");
            forceRefresh();
        }
    }

    public static void launch(Context context) {
        launch(context, "");
    }

    public static void launch(Context context, String payType) {
        Intent intent = new Intent();
        intent.setClass(context, PayRecordActivity.class);
        intent.putExtra("payType", payType);
        context.startActivity(intent);
    }

}
